package com.efounder.frame.utils;

/**
 * Created by james on 2017/3/4.
 */

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.activity.WebViewActivity;
import com.efounder.util.ToastUtil;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * OSP 页面插件接口
 */
public class OSPPlugin {

    private String username;
    private String userpass;
    private Context context;
    private String resultMessage = null;
    private int resultCode = 0;
    private Fragment fragment;

    public OSPPlugin(String username, String userpass, Context context) {
        this.username = username;
        this.userpass = userpass;
        this.context = context;

    }

    public OSPPlugin(String username, String userpass, Fragment fragment) {
        this.username = username;
        this.userpass = userpass;
        this.context = fragment.getContext();
        this.fragment = fragment;

    }

    //打开展示提示信息
    @JavascriptInterface
    public void toast(String str) {
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();
    }

    //页面中打开
    @JavascriptInterface
    public void openFullScreen(String urlstr) {

        Intent intent = new Intent(context, WebViewActivity.class);
        //用Bundle携带数据
        Bundle bundle = new Bundle();
        //传递name参数为tinyphp
        bundle.putString("username", username);
        bundle.putString("userpass", userpass);
        bundle.putString("urlstr", urlstr);
        intent.putExtras(bundle);
        ((Activity) context).startActivity(intent);
    }

    //打开新页面
    @JavascriptInterface
    public void openNewPage(String urlstr) {
        try {
            Intent intent = new Intent();
            intent.setClass(context, Class.forName("com.efounder.chat.activity.ChatWebViewActivity"));
            intent.putExtra("url", urlstr);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showToast(context, "error");
        }
    }

    //拨打电话
    @JavascriptInterface
    public void callPhone(String number) {
        /*Toast.makeText(getActivity(), number, Toast.LENGTH_LONG).show();*/
        //用intent启动拨打电话
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + number);
        intent.setData(data);
        context.startActivity(intent);
    }

    //跳转商城商品详情(参数为商品编号)
    @JavascriptInterface
    public void goodDetail(String goodBh) {
        Intent intent = new Intent();
        try {
            intent.setClass(context, Class.forName("com.efounder.activity.ProductDetailsActivity"));
            intent.putExtra("goodbh", goodBh);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showToast(context, "error");
        }
    }

    //打开扫描二维码
    @JavascriptInterface
    public void openScanQrCode() {
        //跳转扫描二维码
        try {
            Class clazz = null;
            Intent intent = new Intent();
            intent.putExtra("needResult", "true");
            clazz = Class.forName("com.efounder.chat.zxing.qrcode.MipcaActivityCapture");
            intent.setClass(context, clazz);
            if (fragment != null) {
                fragment.startActivityForResult(intent, 1);
            } else {
                ((Activity) context).startActivityForResult(intent, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    //h获取用户密码
    @JavascriptInterface
    public String getUserInfo() {
        String userid = EnvironmentVariable.getUserName();
        String password = EnvironmentVariable.getPassword();
        String imuserid = EnvironmentVariable.getProperty(Constants.CHAT_USER_ID);
        String impassword = EnvironmentVariable.getProperty(Constants.CHAT_PASSWORD);
        //用户头像
        String avatar = EnvironmentVariable.getProperty("im_user_icon", "");
        return userid + ";" + password + ";" + imuserid + ";" + impassword + ";" + avatar;
    }

    //h获取用户密码
    @JavascriptInterface
    public String getEnvValue(String key) {
        String value = EnvironmentVariable.getProperty(key, "");
        return value;
    }

    //h获取用户密码
    @JavascriptInterface
    public void addEnvValue(String key, String value) {
        EnvironmentVariable.setProperty(key, value);
    }

    //模拟http请求
    @JavascriptInterface
    public String httpSend(String urlstr, String type) {

        String inputLine = "";
        HttpURLConnection url_con = null;
        try {
            URL url = new URL(urlstr);
            StringBuffer bankXmlBuffer = new StringBuffer();
            // 创建URL连接，提交到数据，获取返回结果
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);
            connection.setRequestMethod(type);
            connection.setDoOutput(true);
            connection.setRequestProperty("User-Agent", "directclient");
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            while ((inputLine = in.readLine()) != null) {
                bankXmlBuffer.append(inputLine);
            }
            in.close();
            inputLine = bankXmlBuffer.toString();
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            if (url_con != null) url_con.disconnect();
        }
        return inputLine;
    }

    //关闭当前页面
    @JavascriptInterface
    public void closePage() {
        ((Activity) context).finish();
    }

//    //上传文件
//    @JavascriptInterface
//    public String imgUpload(String url,String imgpath,String guid,String imageid){
//        String urlstr=url+"&user="+username+"&pass="+userpass;
//        resultMessage="";
//        String result="";//UploadUtil.uploadFile(new File(imgpath),urlstr);
//        try {
//            EFAppAccountDJZUploadImg.upLoadFileN(context,url, imgpath, guid,imageid);
//        } catch (Exception e) {
//
//        }
//        if(resultMessage.equals("")){
//            SleepForUpload();
//        }
//        result="{\"resultCode\":\""+resultCode+"\",\"imageUrl\":\""+resultMessage+"\",\"message\":\""+resultMessage+"\"}";
//        return result;
//    }
//    //循环等待
//    public void SleepForUpload(){
//        if(resultMessage.equals("")){
//            try{
//                Thread.sleep(100);
//                SleepForUpload();
//            }catch (Exception e) {
//                System.out.println("睡眠出错！" + e);
//                e.printStackTrace();
//            }
//        }
//    }

}
